package com.sitanyun.merchant.guide.model.impl;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.bean.ForgetBean;
import com.sitanyun.merchant.guide.bean.ForgetsmscoedeBean;
import com.sitanyun.merchant.guide.bean.SmsPhoneBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.model.inter.IForgetPasswordAModel;
import com.sitanyun.merchant.guide.presenter.callback.CallBack;
import com.sitanyun.merchant.guide.url.Urls;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;

public class ForgetPasswordAModelImpl implements IForgetPasswordAModel {
    /**
     * 手机号验证
     * @param phone
     * @param callBack
     * @param <T>
     */
    @Override
    public <T> void setLoginphone(String phone, CallBack callBack) {
        OkHttpUtils.get().url(Urls.phoneverification+phone)
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                SmsPhoneBean smsPhoneBean  = new Gson().fromJson(response, SmsPhoneBean.class);
                callBack.onSuccess(smsPhoneBean);
            }
        });
    }
//获取验证码
    @Override
    public <T> void setsmslogin(String mobile, String type, CallBack callBack) {
        OkHttpUtils.get().url(Urls.getsms+"/"+mobile+"/"+type)
                .addHeader("Content-Type","application/x-www-form-urlencoded")
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                SmsPhoneBean smsPhoneBean  = new Gson().fromJson(response, SmsPhoneBean.class);
                callBack.onSuccess(smsPhoneBean);
            }
        });
    }

    /**
     * 验证码验证
     * @param mobile
     * @param sms
     * @param type
     * @param callBack
     * @param <T>
     */
    @Override
    public <T> void setsmslogin(String mobile, String sms, String type, CallBack callBack) {
        OkHttpUtils.get().url(Urls.msgcode+"/"+mobile+"/"+sms+"/"+type)
                .addHeader("Content-Type","application/x-www-form-urlencoded")
               .addHeader("Authorization","Basic dGVzdDp0ZXN0").build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ForgetsmscoedeBean  baseEntity=   new Gson().fromJson(response, ForgetsmscoedeBean.class);
                     callBack.onSuccess(baseEntity);
              }
        });
    }

    /**
     * 手机号码密码
     * @param phone
     * @param sms
     * @param password
     * @param callBack
     * @param <T>
     */
    @Override
    public <T> void setpaswd(String phone, String sms, String password, CallBack callBack) {
        HashMap<String,String> hashMap= new HashMap<String,String>();
        hashMap.put("phone",phone);
        hashMap.put("smsValidateCode",sms);
        hashMap.put("newPassword",password);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type","application/json")
                .addHeader("Authorization","Basic dGVzdDp0ZXN0")
                .url(Urls.checkpaswd)
             .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ForgetBean baseEntity=   new Gson().fromJson(response, ForgetBean.class);
                callBack.onSuccess(baseEntity);
            }
        });
    }
}
